package la.renzhen.rtpt.spring.beans;

import la.renzhen.rtpt.config.ConfigurationProvider;
import la.renzhen.rtpt.spring.boot.RTPTProperties;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 动态配置代理类<p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 2018/05/05
 */
public class ConfigurationBeanFactory<T> implements FactoryBean<T> {

    private Class<T> configurationClass;

    @Autowired
    ConfigurationProvider configurationProvider;

    @Autowired
    RTPTProperties config;

    public void setConfigurationClass(String configurationClass) throws ClassNotFoundException {
        this.configurationClass = (Class<T>) Class.forName(configurationClass);
    }

    @Override
    public T getObject() throws Exception {
        return this.configurationProvider.invoke(configurationClass);
    }

    @Override
    public Class<?> getObjectType() {
        return this.configurationClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
